package ch12_容器框架与泛型;

import java.util.Collection;
import java.util.Random;

public class ProductUtil {			/*随机产生若干产品，打印产品信息*/
	static String[] names = {"洗衣机","手机","电视","电脑","微波炉","空调","冰箱"};
	static Random rand = new Random();

	/*随机打乱names*/
	static {						//静态代码块的特点:随着类的加载而执行，而且只执行一次
		int p,q;
		String temp;
		for (int i = 0; i < rand.nextInt(6)+5; i++) {
			p = rand.nextInt(names.length);
			q = rand.nextInt(names.length);
			temp = names[p];
			names[p] = names[q];
			names[q] = temp;
		}
	}

	public static Product[] createProducts() {
		int count  = rand.nextInt(100) %4 +2;		/*产生2—5个产品*/
		Product[] ps = new Product[count];			/*构造产品数组*/
		for (int i = 0; i < ps.length; i++) {		//生成产品
			Product p = new Product(i+1 ,names[i],rand.nextInt(101)+100);
			ps[i] = p;
		}
		return ps;
	}

	public static void printProducts(Collection<?>  c) {
		c.forEach(o->{
			Product p = (Product) o;		//造型为产品
			if(p!=null) {
				p.print();
			}else {
				System.out.println("空对象");
			}
		});
		System.out.println("-------------------");
	}



}

